package org.komarichin.jdo.persistence;

import com.google.appengine.api.datastore.Blob;
import org.komarichin.beans.IFile;

import javax.jdo.annotations.*;
import java.util.Date;

/**
 * User: Mykhaylo Komarichyn
 * Date: 31.12.2009
 * Time: 10:40:27
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class File implements IFile<Long> {
    private static final long serialVersionUID = -3727677888536096863L;
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
    @Persistent
    private String name;
    @Persistent
    private Blob file;
    @Persistent
    private String contentType;
    @Persistent
    private String type;
    @Persistent
    private Long length;
    @Persistent
    private Date update;
    @Persistent
    private String folder;

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public byte[] getFile() {
        return file.getBytes();
    }

    @Override
    public void setFile(byte[] file) {
        this.file = new Blob(file);
    }

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public boolean isFile() {
        return name != null && file != null;
    }

    @Override
    public boolean isDirectory() {
        return !isFile();
    }

    @Override
    public void setType(String type) {
        this.type = type;
    }

    @Override
    public Long getLength() {
        return length;
    }

    @Override
    public void setLength(Long length) {
        this.length = length;
    }

    @Override
    public Date getUpdate() {
        return update;
    }

    @Override
    public void setUpdate(Date update) {
        this.update = update;
    }

    @Override
    public String getFolder() {
        if (isDirectory()) {
            if ("/".equals(folder)) {
                return folder + name;
            } else {
                return folder + "/" + name;
            }
        } else
            return folder;
    }

    public void setFolder(String folder) {
        this.folder = folder;
    }
}
